package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.ChiTietSanPhamDao;
import com.tt.fashionWebSite.dao.SanPhamFullDao;
import com.tt.fashionWebSite.dao.TapLuatKetHopDao;
import com.tt.fashionWebSite.domain.ChiTietSanPham;
import com.tt.fashionWebSite.domain.ChiTietSanPhamFull;
import com.tt.fashionWebSite.domain.SanPhamFull;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.GenericService;
import com.tt.fashionWebSite.service.ProductService;
import com.tt.fashionWebSite.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by isoft-work 5 on 12/8/2014.
 */
@Service("productService")
public class ProductServiceImp extends GenericService implements ProductService {
   @Autowired
   @Qualifier("sanPhamFull")
   private SanPhamFullDao sanPhamFullDao;

   @Autowired
   @Qualifier("chiTietSanPham")
   private ChiTietSanPhamDao chiTietSanPhamDao;

   @Autowired
   @Qualifier("tapLuatKetHop")
   private TapLuatKetHopDao tapLuatKetHopDao;

//   @Override
//   public List<SanPhamFullDto> getProductByIdDMSP(String gender, String idDMSP) {
//      List<SanPhamFullDto> sanPhamFullDtos = new ArrayList<SanPhamFullDto>();
//      List<SanPhamFull> sanPhamFulls = sanPhamFullDao.getProductByIdDMSP(gender, idDMSP);
//      sanPhamFullDtos = mapper.mapCollection(sanPhamFulls, SanPhamFullDto.class, SAN_PHAM_FULL_MAP_ID);
//      // get size for product
//      List<KichThuocDto> size = new ArrayList<KichThuocDto>();
//      for(SanPhamFullDto temSanPham : sanPhamFullDtos) {
//         List<ChiTietSanPhamFullDto> temChiTietSPs = temSanPham.getChiTietSanPhams();
//         for(ChiTietSanPhamFullDto temCT : temChiTietSPs) {
//            size.add(temCT.getKichThuoc());
//         }
//         temSanPham.setSize(deleteLikeSize(size));
//      }
//      return sanPhamFullDtos;
//   }

   @Override
   public List<SanPhamFullDto> getProductByIdDMSP(String gender, String idDMSP) {
      List<SanPhamFullDto> sanPhamFullDtos = new ArrayList<SanPhamFullDto>();
      sanPhamFullDtos = sanPhamFullDao.getProductByIdDMSP(gender, idDMSP);

      for(SanPhamFullDto temSanPham : sanPhamFullDtos) {
         List<KichThuocDto> size = new ArrayList<KichThuocDto>();
         List<ChiTietSanPhamFullDto> temChiTietSPs = temSanPham.getChiTietSanPhams();
         for(ChiTietSanPhamFullDto temCT : temChiTietSPs) {
            size.add(temCT.getKichThuoc());
         }
         temSanPham.setSize(deleteLikeSize(size));
      }
      return sanPhamFullDtos;
   }

   @Override
   public List<SanPhamFullDto> getProductSearch(String name) {
      List<SanPhamFullDto> sanPhamFullDtos = new ArrayList<SanPhamFullDto>();
      List<SanPhamFull> sanPhamFulls = sanPhamFullDao.getProductSearch(name);
      sanPhamFullDtos = mapper.mapCollection(sanPhamFulls, SanPhamFullDto.class, SAN_PHAM_FULL_MAP_ID);
      // get size for product
      for(SanPhamFullDto temSanPham : sanPhamFullDtos) {
         List<KichThuocDto> size = new ArrayList<KichThuocDto>();
         List<ChiTietSanPhamFullDto> temChiTietSPs = temSanPham.getChiTietSanPhams();
         for(ChiTietSanPhamFullDto temCT : temChiTietSPs) {
            size.add(temCT.getKichThuoc());
         }
         temSanPham.setSize(deleteLikeSize(size));
      }
      return sanPhamFullDtos;
   }

   @Override
   public SanPhamFullDto getProduct(String idSP) {
      SanPhamFull sanPhamFull = sanPhamFullDao.findById(idSP);
      SanPhamFullDto sanPhamFullDto = mapper.map(sanPhamFull, SanPhamFullDto.class, SAN_PHAM_FULL_MAP_ID);

      // get size and color for product
      List<KichThuocDto> size = new ArrayList<KichThuocDto>();
      List<MauSacDto> color = new ArrayList<MauSacDto>();
      List<ChiTietSanPhamFullDto> temChiTietSPs = sanPhamFullDto.getChiTietSanPhams();
      for(ChiTietSanPhamFullDto temCT : temChiTietSPs) {
         size.add(temCT.getKichThuoc());
         color.add(temCT.getMauSac());
      }
      sanPhamFullDto.setSize(deleteLikeSize(size));
      sanPhamFullDto.setMauSac(deleteLikeColor(color));
      return sanPhamFullDto;
   }

   @Override
   public List<SanPhamFullDto> getProductFromRule(String idSP, GioHangDto gioHangDto) {
      List<SanPhamFullDto> sanPhamFullDtos = new ArrayList<SanPhamFullDto>();
      List<TapLuatKetHopDto> tapLuatKetHopDtos = mapper.mapCollection(tapLuatKetHopDao.getAll(), TapLuatKetHopDto.class);
      if(tapLuatKetHopDtos != null && tapLuatKetHopDtos.size() > 0) {
         for (int i = 0; i < tapLuatKetHopDtos.size(); i++) {
            List<String> veTrais = StringUtils.getLeftAndGt(tapLuatKetHopDtos.get(i).getVeTraiMaSP());
            List<String> vePhais = StringUtils.getLeftAndGt(tapLuatKetHopDtos.get(i).getVePhaiMASP());

            for (int j = 0; j < veTrais.size(); j++) {
               if (idSP.equals(veTrais.get(j))) {
//                     add vế trái
                  for (int m = 0; m < veTrais.size(); m++) {
                     SanPhamFullDto sanPhamFullDto = mapper.map(sanPhamFullDao.findById(veTrais.get(m)), SanPhamFullDto.class, SAN_PHAM_FULL_MAP_ID);
                     sanPhamFullDtos.add(sanPhamFullDto);
                  }
//                add vế phải
                  for (int n = 0; n < vePhais.size(); n++) {
                     SanPhamFullDto sanPhamFullDto = mapper.map(sanPhamFullDao.findById(vePhais.get(n)), SanPhamFullDto.class, SAN_PHAM_FULL_MAP_ID);
                     sanPhamFullDtos.add(sanPhamFullDto);
                  }
               }
            }
         }

         deleteProducLike(sanPhamFullDtos, idSP);

         List<KichThuocDto> size = new ArrayList<KichThuocDto>();
         for (SanPhamFullDto temSanPham : sanPhamFullDtos) {
            List<ChiTietSanPhamFullDto> temChiTietSPs = temSanPham.getChiTietSanPhams();
            for (ChiTietSanPhamFullDto temCT : temChiTietSPs) {
               size.add(temCT.getKichThuoc());
            }
            temSanPham.setSize(deleteLikeSize(size));
         }
         return sanPhamFullDtos;
      }

      return null;
   }

   private void deleteProducLike(List<SanPhamFullDto> sanPhamFullDtos, String idSP) {
      for (int i = 0; i < sanPhamFullDtos.size() - 1; i ++) {
         if(sanPhamFullDtos.get(i).getMaSP().equals(idSP)) {
            sanPhamFullDtos.remove(i);
            i -= 1;
            continue;
         }
         for(int j = i + 1; j < sanPhamFullDtos.size(); j++) {
            if(sanPhamFullDtos.get(j).getMaSP().equals(idSP) || sanPhamFullDtos.get(j).getMaSP().equals(sanPhamFullDtos.get(i).getMaSP())){
               sanPhamFullDtos.remove(j);
               i -= 1;
               j -= 1;
               break;
            }
         }
      }
   }

   private List<KichThuocDto> deleteLikeSize(List<KichThuocDto> listSize) {
      List<KichThuocDto> result = listSize;
      if(listSize == null) {
         return null;
      }
      else {
         if (listSize.size() == 1 ){
            return result;
         }
         else {
            for(int j = 0; j < listSize.size() - 1; j++) {
               for(int i = j + 1; i < listSize.size(); i++) {
                  if(listSize.get(i).getMaSize().equals(listSize.get(j).getMaSize())) {
                     listSize.remove(i);
                     result = deleteLikeSize(listSize);
                  }
               }
            }
            return result;
         }
      }
   }

   private List<MauSacDto> deleteLikeColor(List<MauSacDto> listColor) {
      List<MauSacDto> result = listColor;
      if(listColor == null) {
         return null;
      }
      else {
         if (listColor.size() == 1 ){
            return result;
         }
         else {
            for(int j = 0; j < listColor.size() - 1; j++) {
               for(int i = j + 1; i < listColor.size(); i++) {
                  if(listColor.get(i).getMaMau().equals(listColor.get(j).getMaMau())) {
                     listColor.remove(i);
                     result = deleteLikeColor(listColor);
                  }
               }
            }
            return result;
         }
      }
   }

   @Override
   public GioHangDto addCart(ChiTietSanPhamFullDto chiTietSanPhamFull, GioHangDto gioHang) {
      GioHangDto result = new GioHangDto();
      List<CartDto> carts = new ArrayList<CartDto>();
      CartDto cart = new CartDto();
      ChiTietSanPhamDto chiTietSanPham = new ChiTietSanPhamDto();
      Date now = new Date();

      if(gioHang != null && gioHang.getTotal() > 0) {
         //      check enough number product
         result = gioHang;
         carts = gioHang.getCarts();
         Integer countShop = gioHang.getTotal();
         Double toTalMoney = gioHang.getTotalMoney();
         String idCTSP = chiTietSanPhamFull.getMaCTSP();
         for (int i = 0; i < carts.size(); i++) {
            cart = carts.get(i);
            if(cart.getChiTietSanPham().getMaCTSP().equals(idCTSP)) {
               if(checkNumberProductUpdate(chiTietSanPhamFull, gioHang)) {
                  //               update money
                  toTalMoney += cart.getGiaBan();
                  result.setTotalMoney(toTalMoney);

//               update cart
                  cart.setCount(cart.getCount() + 1);
                  carts.remove(i);
                  carts.add(cart);

//               update shop cart
                  countShop += 1;
                  result.setCarts(carts);
                  result.setTotal(countShop);
                  break;
               }
               else {
                  return null;
               }
            }
//           add new
            else {
               cart = new CartDto();
               if(i == carts.size() - 1) {
                  if(checkNumberProductAdd(chiTietSanPhamFull)) {
                     //               set totalMoney and cost
                     if(chiTietSanPhamFull.getChiTietKhuyenMai() != null && chiTietSanPhamFull.getChiTietKhuyenMai().size() > 0) {
                        Date begin = chiTietSanPhamFull.getChiTietKhuyenMai().get(0).getKhuyenMai().getNgayBD();
                        Date end = chiTietSanPhamFull.getChiTietKhuyenMai().get(0).getKhuyenMai().getNgayKT();
                        if((now.equals(begin) || now.after(begin)) && (now.equals(end) || now.before(end))) {
                           Long temGia = Math.round(chiTietSanPhamFull.getGiaSP()) - Math.round(chiTietSanPhamFull.getGiaSP() * chiTietSanPhamFull.getChiTietKhuyenMai().get(0).getPhanTram() / 100);
                           Double giaBan = Double.parseDouble(temGia.toString());
                           cart.setGiaBan(giaBan);
                        }
                        else {
                           cart.setGiaBan(chiTietSanPhamFull.getGiaSP());
                        }
                     }
                     else {
                        cart.setGiaBan(chiTietSanPhamFull.getGiaSP());
                     }
                     cart.setCount(1);
                     cart.setChiTietSanPham(chiTietSanPhamFull);

                     ChiTietSanPham chiTietSanPham1 = chiTietSanPhamDao.findById(chiTietSanPhamFull.getMaCTSP());
                     cart.setTenSP(chiTietSanPham1.getSanPham().getTenSP());
                     if(chiTietSanPhamFull.getHinhAnhSanPham() != null && chiTietSanPhamFull.getHinhAnhSanPham().size() > 0) {
                        cart.setImage(chiTietSanPhamFull.getHinhAnhSanPham().get(0).getTenHinh());
                     }
                     carts.add(cart);
                     countShop = carts.get(i).getCount() + 1;
                     toTalMoney += cart.getGiaBan();

                     result.setTotalMoney(toTalMoney);
                     result.setCarts(carts);
                     result.setTotal(countShop);
                     break;
                  }
                  else {
                     return null;
                  }
               }
            }
         }

      }
//    add first cart
      else {
         if(checkNumberProductAdd(chiTietSanPhamFull)) {
            //       set totalMoney and cost
            if(chiTietSanPhamFull.getChiTietKhuyenMai() != null && chiTietSanPhamFull.getChiTietKhuyenMai().size() > 0) {
               Date begin = chiTietSanPhamFull.getChiTietKhuyenMai().get(0).getKhuyenMai().getNgayBD();
               Date end = chiTietSanPhamFull.getChiTietKhuyenMai().get(0).getKhuyenMai().getNgayKT();
               if((now.equals(begin) || now.after(begin)) && (now.equals(end) || now.before(end))) {
                   Long temGia = Math.round(chiTietSanPhamFull.getGiaSP()) - Math.round(chiTietSanPhamFull.getGiaSP() * chiTietSanPhamFull.getChiTietKhuyenMai().get(0).getPhanTram() / 100);
                  Double giaBan = Double.parseDouble(temGia.toString());
                  cart.setGiaBan(giaBan);
               }
               else {
                  cart.setGiaBan(chiTietSanPhamFull.getGiaSP());
               }
            }
            else {
               cart.setGiaBan(chiTietSanPhamFull.getGiaSP());
            }
            ChiTietSanPham chiTietSanPham1 = chiTietSanPhamDao.findById(chiTietSanPhamFull.getMaCTSP());
            cart.setTenSP(chiTietSanPham1.getSanPham().getTenSP());
            if(chiTietSanPhamFull.getHinhAnhSanPham() != null && chiTietSanPhamFull.getHinhAnhSanPham().size() > 0) {
               cart.setImage(chiTietSanPhamFull.getHinhAnhSanPham().get(0).getTenHinh());
            }
            cart.setCount(1);
            cart.setChiTietSanPham(chiTietSanPhamFull);

            carts.add(cart);
            result.setCarts(carts);
            result.setTotal(1);
            result.setTotalMoney(cart.getGiaBan());
         }
         else {
            return null;
         }
      }

      return result;
   }

   private boolean checkNumberProductUpdate(ChiTietSanPhamFullDto chiTietSanPhamFull, GioHangDto gioHang) {
      boolean result = false;
      List<CartDto> cartDtos = gioHang.getCarts();
      for(int i = 0; i < cartDtos.size(); i++) {
         String idCTSP = cartDtos.get(i).getChiTietSanPham().getMaCTSP();
         if(idCTSP.equals(chiTietSanPhamFull.getMaCTSP())) {
            Long num1 = chiTietSanPhamFull.getSoLuong();
            Integer num2 = cartDtos.get(i).getCount() + 1;
            if(num1 >= num2) {
               result = true;
            }
            else {
               result = false;
            }
            break;
         }
      }
      return result;
   }

   private boolean checkNumberProductAdd(ChiTietSanPhamFullDto chiTietSanPhamFull) {
      boolean result = false;
      if(chiTietSanPhamFull.getSoLuong() - 1 >= 0 ) {
         result = true;
      }
      return result;
   }

   @Override
   public GioHangDto updateCart(String idCTSP,Integer num, GioHangDto gioHang){
      List<CartDto> carts = gioHang.getCarts();
      ChiTietSanPham chiTietSanPham2 = chiTietSanPhamDao.findById(idCTSP);
      if(chiTietSanPham2.getSoLuong() >= num) {
         for(int i = 0; i < carts.size(); i++) {
            if(carts.get(i).getChiTietSanPham().getMaCTSP().equals(idCTSP)) {
               // update gio hang
               Integer toTalCount = gioHang.getTotal();
               Double toTalMoney = gioHang.getTotalMoney();
               toTalCount = toTalCount + num - carts.get(i).getCount();
               toTalMoney = toTalMoney + num * carts.get(i).getGiaBan() - carts.get(i).getCount() * carts.get(i).getGiaBan();

               gioHang.setTotalMoney(toTalMoney);
               gioHang.setTotal(toTalCount);

//            update cart
               carts.get(i).setCount(num);
               break;
            }
         }

         return gioHang;
      }
      return null;
   }

   @Override
   public GioHangDto deleteCart(String idCTSP, GioHangDto gioHang){
      List<CartDto> carts = gioHang.getCarts();
      for(int i = 0; i < carts.size(); i++) {
         if(carts.get(i).getChiTietSanPham().getMaCTSP().equals(idCTSP)) {
            // update gio hang
            Integer toTalCount = gioHang.getTotal();
            Double toTalMoney = gioHang.getTotalMoney();
            toTalCount = toTalCount - carts.get(i).getCount();
            toTalMoney = toTalMoney - carts.get(i).getCount()*carts.get(i).getGiaBan();

            gioHang.setTotalMoney(toTalMoney);
            gioHang.setTotal(toTalCount);

//            update cart
            carts.remove(i);
            break;
         }
      }
      return gioHang;
   }
}
